package com.zj.fx.controller.month;

import com.github.pagehelper.PageInfo;
import com.zj.fx.common.base.BaseController;
import com.zj.fx.common.domain.AjaxResult;
import com.zj.fx.common.domain.ResultTable;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.model.month.MonthConstant;
import com.zj.fx.model.month.MonthNecessary;
import com.zj.fx.service.month.MonthNecessaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 必备条件指标库表Controller
 * @ClassName: MonthNecessaryController
 * @author SQL
 * @date 2023-06-13 15:02:42
 */
@Api(value = "必备条件指标库表")
@Controller
@RequestMapping("/MonthNecessaryController")
public class MonthNecessaryController extends BaseController {

    private String prefix = "month/monthNecessary";

    @Autowired
    private MonthNecessaryService monthNecessaryService;


    /**
     * 必备条件指标库表页面展示
     * @param model
     * @return String
     * @author SQL
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("gen:monthNecessary:view")
    public String view(ModelMap model) {
        return prefix + "/list";
    }

    /**
     * list集合
     * @param tablepar
     * @param
     * @return
     */
    //@Log(title = "必备条件指标库表", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list")
    @RequiresPermissions("gen:monthNecessary:list")
    @ResponseBody
    public ResultTable list(Tablepar tablepar, MonthNecessary monthNecessary) {
        PageInfo<MonthNecessary> page = monthNecessaryService.list(tablepar,monthNecessary);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 新增跳转
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }

    /**
     * 新增保存
     * @param 
     * @return
     */
    //@Log(title = "必备条件指标库表新增", action = "111")
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    @RequiresPermissions("gen:monthNecessary:add")
    @ResponseBody
    public AjaxResult add(MonthNecessary monthNecessary) {
        int b = monthNecessaryService.insertSelective(monthNecessary);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 必备条件指标库表删除
     * @param ids
     * @return
     */
    //@Log(title = "必备条件指标库表删除", action = "111")
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/remove")
    @RequiresPermissions("gen:monthNecessary:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = monthNecessaryService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }


    /**
     * 修改跳转
     * @param id
     * @param
     * @return
     */
    @ApiOperation(value = "修改跳转", notes = "修改跳转")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map) {
        map.put("MonthNecessary", monthNecessaryService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    //@Log(title = "必备条件指标库表修改", action = "111")
    @ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:monthNecessary:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MonthNecessary monthNecessary) {
        return toAjax(monthNecessaryService.updateByPrimaryKeySelective(monthNecessary));
    }

    /**
     * list集合--当前登陆人员供电所下的月度必备项目
     * @param
     * @param
     * @return
     */
    //@Log(title = "必备条件指标库表", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list2/{taskId}")
    @ResponseBody
    public ResultTable list2(@PathVariable("taskId")String taskId,Tablepar tablepar, MonthNecessary monthNecessary) {
        monthNecessary.setFkTaskId(taskId);
        PageInfo<MonthNecessary> page = monthNecessaryService.list2(tablepar,monthNecessary);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 更新月度必备项目自评价结果
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "更新月度必备项目自评价结果", notes = "更新月度必备项目自评价结果")
    @PostMapping("/updateSelf")
    @ResponseBody
    public AjaxResult updateSelf(@RequestBody MonthNecessary monthNecessary) {
        String id = monthNecessary.getId();
        Integer typeValue = monthNecessary.getTypeSelf();
        monthNecessary = monthNecessaryService.selectByPrimaryKey(id);
        monthNecessary.setTypeSelf(typeValue);
        return toAjax(monthNecessaryService.updateByPrimaryKeySelective(monthNecessary));
    }

    /**
     * list集合--当前登陆人员部门下负责的月度必备项目
     * @param
     * @param
     * @return
     */
    //@Log(title = "必备条件指标库表", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/listbyDep/{taskId}")
    @ResponseBody
    public ResultTable listbyDep(@PathVariable("taskId")String taskId,Tablepar tablepar, MonthNecessary monthNecessary) {
        monthNecessary.setFkTaskId(taskId);
        PageInfo<MonthNecessary> page = monthNecessaryService.listbyDep(tablepar,monthNecessary);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 更新月度必备项目负责部门审核结果
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "更新月度必备项目负责部门审核结果", notes = "更新月度必备项目负责部门审核结果")
    @PostMapping("/updateAudit")
    @ResponseBody
    public AjaxResult updateAudit(@RequestBody MonthNecessary monthNecessary) {
        String id = monthNecessary.getId();
        Integer typeValue = monthNecessary.getTypeReview();
        monthNecessary = monthNecessaryService.selectByPrimaryKey(id);
        //如果数据已经被审核过，结果不相同，此时说明要改为结果与自评值相同
        if (MonthConstant.IS.equals(monthNecessary.getIsAuditedit())){
            monthNecessary.setTypeReview(2);
            monthNecessary.setIsAuditedit(MonthConstant.NOT);
        }else{
            if (monthNecessary.getTypeSelf()==typeValue){
                return AjaxResult.error("更正值与自评值相同，无需更正");
            }else {
                monthNecessary.setTypeReview(typeValue);
                monthNecessary.setIsAuditedit(MonthConstant.IS);
            }
        }
        return toAjax(monthNecessaryService.updateByPrimaryKeySelective(monthNecessary));
    }

    /**
     * 更新月度必备项目分公司审核结果
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "更新月度必备项目分公司审核结果", notes = "更新月度必备项目分公司审核结果")
    @PostMapping("/updateSubAudit")
    @ResponseBody
    public AjaxResult updateSubAudit(@RequestBody MonthNecessary monthNecessary) {
        String id = monthNecessary.getId();
        Integer typesubValue = monthNecessary.getTypeSubreview();
        monthNecessary = monthNecessaryService.selectByPrimaryKey(id);
        //如果数据已经被审核过，结果不相同，此时说明要改为结果与自评值相同
        if (MonthConstant.IS.equals(monthNecessary.getIsSubauditedit())){
            monthNecessary.setTypeSubreview(2);
            monthNecessary.setIsSubauditedit(MonthConstant.NOT);
        }else{
            if (monthNecessary.getTypeSelf()==typesubValue){
                return AjaxResult.error("更正值与自评值相同，无需更正");
            }else {
                monthNecessary.setTypeSubreview(typesubValue);
                monthNecessary.setIsSubauditedit(MonthConstant.IS);
            }
        }
        return toAjax(monthNecessaryService.updateByPrimaryKeySelective(monthNecessary));
    }

    /**
     * list集合--供电所自评价完成后查询结果
     * @param
     * @param
     * @return
     */
    //@Log(title = "必备条件指标库表", action = "111")
    @ApiOperation(value = "供电所自评价完成后查询结果", notes = "供电所自评价完成后查询结果")
    @GetMapping("/list4selfResult/{taskId}")
    @ResponseBody
    public ResultTable list4selfResult(@PathVariable("taskId")String taskId,Tablepar tablepar, MonthNecessary monthNecessary) {
        monthNecessary.setFkTaskId(taskId);
        PageInfo<MonthNecessary> page = monthNecessaryService.list4selfResult(tablepar,monthNecessary);
        return pageTable(page.getList(), page.getTotal());
    }
}
